package com.redisdemo.controller;

import com.redisdemo.utlis.RSAUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * <h3>redisDemo</h3>
 * <p></p>
 *
 * @author gdw
 * @since 2024-07-02 14:09:51
 */
@RestController
@RequestMapping("/rsaUtils")
public class RSAUtilsController {

    public static void main(String[] args) {
        try {
            KeyPair keyPair = RSAUtils.generateKeyPair();
            String publicKeyString = RSAUtils.getKeyString(keyPair.getPublic());
            String privateKeyString = RSAUtils.getKeyString(keyPair.getPrivate());

            System.out.println("Public Key: " + publicKeyString);
            System.out.println("Private Key: " + privateKeyString);
            // 自定义的公钥和私钥字符串（请使用你的实际密钥字符串）
            String customPublicKeyString = publicKeyString;  //公钥
            String customPrivateKeyString = privateKeyString;  //密钥

            // 从字符串转换为公钥和私钥对象
            PublicKey customPublicKey = RSAUtils.getPublicKey(customPublicKeyString);
            PrivateKey customPrivateKey = RSAUtils.getPrivateKey(customPrivateKeyString);

            // 测试数据
            String originalText = "Hello, RSA!";
            System.out.println("Original Text: " + originalText);

            // 使用自定义公钥加密
            String encryptedText = RSAUtils.encrypt(originalText, customPublicKey);
            System.out.println("Encrypted Text: " + encryptedText);

            // 使用自定义私钥解密
            String decryptedText = RSAUtils.decrypt(encryptedText, customPrivateKey);
            System.out.println("Decrypted Text: " + decryptedText);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
